package com.cloudsigns.factorymanager2.media_system.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.media_system.fragment.MediaSystemAlarmWindowFragment;
import com.cloudsigns.factorymanager2.media_system.fragment.MediaSystemRemoteControlFragment;
import com.cloudsigns.factorymanager2.produce.utils.ProduceForTeamEvent;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

public class MediaSystemEquipmentCommissionFragmentActivity extends FragmentActivity implements View.OnClickListener{
    private RelativeLayout rl_back;
    private TextView tv_title;
    private View ll_remote_control, ll_alarm_window;
    private TextView tv_remote_control, tv_alarm_window;
    private CustomViewPager viewpager;
    private View rl_progressbar;
    public static final int REMOTE_CONTROL = 0;
    public static final int ALARM_WINDOW = 1;
    public int currentIndex = REMOTE_CONTROL;

    private MediaSystemRemoteControlFragment remoteControlFragment;
    private MediaSystemAlarmWindowFragment alarmWindowFragment;
    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_media_system_equipment_commission);


        initView();

        tv_title.setText(getResources().getText(R.string.receive_order));
        EventBus.getDefault().register(this);// 注册

        initData();
    }

    private void initView() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        rl_progressbar = findViewById(R.id.rl_progressbar);
        viewpager = findViewById(R.id.viewpager);
        ll_remote_control = findViewById(R.id.ll_remote_control);
        ll_alarm_window = findViewById(R.id.ll_alarm_window);
        tv_remote_control = findViewById(R.id.tv_remote_control);
        tv_alarm_window = findViewById(R.id.tv_alarm_window);

        rl_back.setOnClickListener(this);
        ll_remote_control.setOnClickListener(this);
        ll_alarm_window.setOnClickListener(this);

        remoteControlFragment = new MediaSystemRemoteControlFragment();
        alarmWindowFragment = new MediaSystemAlarmWindowFragment();

        listFragments = new ArrayList<Fragment>();
        listFragments.add(remoteControlFragment);
        listFragments.add(alarmWindowFragment);

        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(2);

        PageIndicatorView indicator =  findViewById(R.id.indicator);
        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.rgb_2E75B6));
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    /**
     * 从服务器获取数据
     */
    private void initData() {

    }

    /**
     * 切换到哪个tab页
     *
     * @param pos
     */
    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            // 远程控制
            case REMOTE_CONTROL:
                ll_remote_control.setSelected(true);
                ll_alarm_window.setSelected(false);

                tv_remote_control.setTextColor(getResources().getColor(R.color.rgb_3333333));
                tv_alarm_window.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;
            // 报警窗口
            case ALARM_WINDOW:
                ll_remote_control.setSelected(false);
                ll_alarm_window.setSelected(true);

                tv_remote_control.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_alarm_window.setTextColor(getResources().getColor(R.color.rgb_3333333));
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 远程控制
            case R.id.ll_remote_control:
                if (currentIndex != REMOTE_CONTROL) {
                    currentIndex = REMOTE_CONTROL;
                    viewpager.setCurrentItem(REMOTE_CONTROL);
                    showSelectedOption(REMOTE_CONTROL);
                }
                break;
            // 报警窗口
            case R.id.ll_alarm_window:
                if (currentIndex != ALARM_WINDOW) {
                    currentIndex = ALARM_WINDOW;
                    viewpager.setCurrentItem(ALARM_WINDOW);
                    showSelectedOption(ALARM_WINDOW);
                }
                break;
        }
    }

    public void onEventMainThread(ProduceForTeamEvent event) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//解注册
    }
}
